package com.first.computerstore.controller;

import com.first.computerstore.entity.Favorites;
import com.first.computerstore.query.PageInfoQuery;
import com.first.computerstore.service.FavoritesService;
import com.first.computerstore.service.ProductService;
import com.first.computerstore.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("favorites")
public class FavoritesController extends BaseController{
    @Resource
    private FavoritesService favoritesService;
    @Resource
    private ProductService productService;

    /*收藏*/
    /*查询当前用户收藏的商品*/
    @GetMapping("queryFavorites")
    public JsonResult<PageInfo<Favorites>> queryFavorites(Integer status,PageInfoQuery pageInfoQuery,HttpSession session){
        //通过session作用域查询当前登录用户的id
        Integer uid=getUserIdFromSession(session);
        //调用service层的方法queryFavorites,得到查询的泛型类
        PageInfo<Favorites> favorites =favoritesService.queryFavorites(status,uid,pageInfoQuery);

        return new JsonResult<>(OK,favorites);
    }
    /*添加收藏功能*/
    @PostMapping("addFavorites")
    public JsonResult<Integer> addFavorites(Integer pid, HttpSession session){
        //通过session作用域查询当前登录用户的id
        Integer uid=getUserIdFromSession(session);
        /*AssertUtil.isTrue(null==uid,"用户不存在，请重新登陆");*/
        int fid = favoritesService.addFavorites(pid, uid);

        return new JsonResult<Integer>(200,fid);
    }
    /*取消收藏*/
    @PostMapping("updateStatus")
    public JsonResult cancelFavorites(HttpSession session,Integer fid){
        Integer uid=getUserIdFromSession(session);
        //调用service方法
        favoritesService.cancelFavorites(uid,fid);
        return new JsonResult<>(OK);
    }

    /**
     * 商品详情页 通过商品id查询是否已经加入购物车中
     * @param pid
     * @return
     */
    @GetMapping("/queryFavoritesByPid/{pid}")
    public JsonResult<Integer> queryFavoritesByPid(@PathVariable Integer pid,HttpSession session) {
        Integer uid = getUserIdFromSession(session);
        return favoritesService.queryFavoritesByPid(uid,pid);
    }
}
